package com.apes.fn.scm.lossSale;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.query.ApesDao;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @program:
 * @description:
 * @author: lws
 * @create: 2023/3/17 10:52
 **/
@Component
public class LossSaleReturnDao extends ApesDao {

    public JSONArray findAll(JSONObject param) {
        String sSQL = "SELECT PAGE.*\n" +
                "       FROM (SELECT TAB.*, ROWNUM AS NUM\n" +
                "      FROM (";
        sSQL += querySQL(param,false);
        sSQL += "   ) TAB\n" +
                "      WHERE ROWNUM <= :max) PAGE\n" +
                "   WHERE PAGE.NUM >= :min";
        return toJSONArray(find(sSQL, param));
    }

    public JSONArray findCount(JSONObject param) {
        return toJSONArray(find(querySQL(param,true), param));
    }

    private String querySQL(JSONObject param,boolean isCount){
        String sql;
        if (!isCount) {
            sql = "select a.ID ID\n" +
                    "               from loss_Sale_return a\n" +
                    "               where a.STATE <> 'delete'\n";
        } else {
            sql = "select count(1) COUNT\n" +
                    "               from loss_Sale_return a\n" +
                    "               where a.STATE <> 'delete'\n";
        }
        if (param.containsKey("createDateGTE") && param.containsKey("createDateLTE"))
            sql += " and a.CREATE_DATE >= :createDateGTE and a.CREATE_DATE <= :createDateLTE\n";
        if (param.containsKey("dateApproveGTE") && param.containsKey("dateApproveLTE"))
            sql += " and a.date_approve >= :dateApproveGTE and a.date_approve <= :dateApproveLTE\n";
        if (param.containsKey("lossQuotationId"))
            sql += " and a.loss_Quotation_Id = :lossQuotationId\n";
        if (param.containsKey("customerExpand.carLicense"))
            sql += " and (select m.CAR_LICENSE from fn_customer_expand m where m.id = a.CUSTOMER_EXPAND_ID) = :customerExpand.carLicense\n";
        if (param.containsKey("dept.id"))
            sql += " and a.dept_id = :dept.id\n";
        if (param.containsKey("state"))
            sql += " and a.state = :state\n";
        if (param.containsKey("id"))
            sql += " and (a.id = :id or a.loss_Quotation_Id = :id or a.retail_id = :id)\n";
        if (param.containsKey("postId"))
            sql += " and ((EXISTS(SELECT 1\n" +
                "                              FROM AUT_POSTS ROLE1,\n" +
                "                                   AUT_LOCAL_PERMIT ROLE2,\n" +
                "                                   AUT_LOCAL_PERMIT_ITEM ROLE3\n" +
                "                              WHERE ROLE1.LOCAL_PERMIT_ID = ROLE2.ID\n" +
                "                                AND ROLE2.ID = ROLE3.LOCAL_PERMIT_ID\n" +
                "                                AND ROLE1.ID = :postId\n" +
                "                                AND ROLE3.TYPE = 'pt_dept'\n" +
                "                                AND ROLE3.PURPOSE = 'pp_search'\n" +
                "                                AND A.DEPT_ID = ROLE3.ORG_ID) OR (SELECT NVL(COUNT(1), 0)\n" +
                "                                                                  FROM AUT_POSTS ROLE1,\n" +
                "                                                                       AUT_LOCAL_PERMIT ROLE2,\n" +
                "                                                                       AUT_LOCAL_PERMIT_ITEM ROLE3\n" +
                "                                                                  WHERE ROLE1.LOCAL_PERMIT_ID = ROLE2.ID\n" +
                "                                                                    AND ROLE2.ID = ROLE3.LOCAL_PERMIT_ID\n" +
                "                                                                    AND ROLE1.ID = :postId\n" +
                "                                                                    AND ROLE3.TYPE = 'pt_dept'\n" +
                "                                                                    AND ROLE3.PURPOSE = 'pp_search') = 0))";
        return sql;
    }
}
